package pl.edu.agh.ki.ewywiadowka.client.windows.secretary;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.Const.Error.AttendantModifyMsg;
import pl.edu.agh.ki.ewywiadowka.client.dto.AttendantDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.UserDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.GenericWindow;

public class ModifyAttendantWindow extends GenericWindow implements
		ClickHandler {

	private VerticalPanel _attendantPanel = new VerticalPanel();

	private String[] _attendantFields = { "Imię", "Nazwisko", "Adres",
			"Telefon", "Zaklad pracy", "e-mail", "Login", "Haslo" };

	public TextBox _attendantNameTextBox = new TextBox();
	public TextBox _attendantSurnameTextBox = new TextBox();
	public TextBox _attendantAddressTextBox = new TextBox();
	public TextBox _attendantTelephoneTextBox = new TextBox();
	public TextBox _attendantWorkTextBox = new TextBox();

	public TextBox _attendantEmailTextBox = new TextBox();
	public TextBox _attendantLoginTextBox = new TextBox();
	public TextBox _attendantPassTextBox = new TextBox();

	private Grid _attendantFormular = null;

	public Button _okButton = new Button("Modyfikuj opiekuna");

	private AttendantDTO _attendant;

	public ModifyAttendantWindow(EWywiadowka wywiadowka, AttendantDTO attendant) {
		super(wywiadowka);

		_attendant = attendant;

		_attendantFormular = new Grid(_attendantFields.length, 2);
		for (int i = 0; i < _attendantFields.length; i++)
			_attendantFormular.setWidget(i, 0, new Label(_attendantFields[i]));

		int j = 0;
		_attendantFormular.setWidget(j++, 1, _attendantSurnameTextBox);
		_attendantFormular.setWidget(j++, 1, _attendantNameTextBox);
		_attendantFormular.setWidget(j++, 1, _attendantAddressTextBox);
		_attendantFormular.setWidget(j++, 1, _attendantTelephoneTextBox);
		_attendantFormular.setWidget(j++, 1, _attendantWorkTextBox);
		_attendantFormular.setWidget(j++, 1, _attendantEmailTextBox);
		_attendantFormular.setWidget(j++, 1, _attendantLoginTextBox);
		_attendantFormular.setWidget(j++, 1, _attendantPassTextBox);

		_attendantSurnameTextBox.setText(attendant.surname);
		_attendantNameTextBox.setText(attendant.name);
		_attendantAddressTextBox.setText(attendant.address);
		_attendantTelephoneTextBox.setText(attendant.telephone.toString());
		_attendantWorkTextBox.setText(attendant.work);
//		_attendantLoginTextBox.setText(attendant.user.login);
//		_attendantPassTextBox.setText(attendant.user.pass);
//		_attendantEmailTextBox.setText(attendant.user.email);

		
		_okButton.addClickHandler(this);
		_attendantFormular.setBorderWidth(2);

		_attendantPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		_attendantPanel.add(_attendantFormular);
		_attendantPanel.add(_okButton);

		this.initWidget(_attendantPanel);

	}

	public Const.FormularMessages verifyFormular() {

		if (_attendantSurnameTextBox.getText().length() == 0)
			return Const.FormularMessages.WRONG_ATTENDANT_SURNAME;
		if (_attendantNameTextBox.getText().length() == 0)
			return Const.FormularMessages.WRONG_ATTENDANT_NAME;
		if (_attendantAddressTextBox.getText().length() == 0)
			return Const.FormularMessages.WRONG_ATTENDANT_ADDRESS;
		try {
			if (_attendantTelephoneTextBox.getText().length() == 0)
				return Const.FormularMessages.WRONG_ATTENDANT_PHONE;
			Integer.parseInt(_attendantTelephoneTextBox.getText());
		} catch (NumberFormatException e) {
			return Const.FormularMessages.WRONG_ATTENDANT_PHONE;
		}

		if (_attendantLoginTextBox.getText().length() == 0)
			return Const.FormularMessages.WRONG_LOGIN;

		if (_attendantPassTextBox.getText().length() == 0)
			return Const.FormularMessages.WRONG_PASS;

		return Const.FormularMessages.OK;
	}

	public void attendantModify() {
		
		UserDTO user = new UserDTO(_attendantLoginTextBox.getText(), _attendantPassTextBox.getText(), _attendantEmailTextBox.getText(), _attendant.user.attr);
		
		AttendantDTO attendant = new AttendantDTO(_attendant.id, _attendantNameTextBox.getText(), _attendantSurnameTextBox.getText(), _attendantAddressTextBox.getText(), _attendantTelephoneTextBox.getText(), _attendantWorkTextBox.getText(), user, _attendant.students);
		
		Const.eWywiadowkaService.modifyAttendant(attendant, new AsyncCallback<AttendantModifyMsg>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(AttendantModifyMsg result) {
				Window.alert(result.toString());
			}
			
		});
	}

	public void onClick(ClickEvent event) {
		Const.FormularMessages msg = null;
		if ((msg = verifyFormular()) == Const.FormularMessages.OK)
			attendantModify();
		else
			Window.alert(msg.toString());
	}

}
